package org.android.brasil.projetos.oktoberfest2011.view;

import org.android.brasil.projetos.oktoberfest2011.R;
import org.android.brasil.projetos.oktoberfest2011.utils.AnalyticsUtils;
import org.android.brasil.projetos.oktoberfest2011.utils.Constantes;
import org.android.brasil.projetos.oktoberfest2011.utils.ControleDeBackground;
import org.android.brasil.projetos.oktoberfest2011.view.controller.HomeController;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnErrorListener;
import android.media.MediaPlayer.OnPreparedListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.MediaController;
import android.widget.RelativeLayout;
import android.widget.VideoView;

public class CameraView extends Activity {
	private VideoView video;
	private MediaController ctlr;
	private AnalyticsUtils mGa;
	private ProgressDialog progressDialog;
	private static Thread t;
	private String url;

	private OnClickListener casaListener = new OnClickListener() {

		public void onClick(View v) {

			if (video != null && video.isPlaying()) {
				video.stopPlayback();
			}

			Intent i = new Intent(getApplicationContext(), HomeController.class);
			i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			startActivity(i);
			mGa.buttonTrackEvent("Casa");

		}
	};

	@Override
	protected void onPause() {
		super.onPause();
		if (video != null && video.isPlaying()) {
			video.stopPlayback();
		}

		if (progressDialog != null && progressDialog.isShowing()) {
			progressDialog.dismiss();
		}

		if (t != null && t.isAlive()) {
			t.stop();

		}
	}

	@Override
	protected void onResume() {
		super.onResume();

		if (video != null && video.isPlaying()) {
			video.stopPlayback();
		}
		
		ctlr = new MediaController(CameraView.this);
		
		String cam = getIntent().getStringExtra("CAMERA");

		if (cam != null && cam.equals(Constantes.SETOR1)) {

			url = "rtsp://m.nethorizontes.tv/as113bnu1fms/bnusc1";

		} else if (cam != null && cam.equals(Constantes.SETOR2)) {

			url = "rtsp://m.nethorizontes.tv/as113bnu1fms/bnusc1";

		} else if (cam != null && cam.equals(Constantes.SETOR3)) {

			url = "rtsp://m.nethorizontes.tv/as113bnu1fms/bnusc1";

		}

		video = (VideoView) findViewById(R.id.video);


		ctlr.setMediaPlayer(video);
		video.setMediaController(ctlr);

		video.setVideoPath(url);
		video.requestFocus();
		
		
		progressDialog = ProgressDialog.show(CameraView.this, "Carregando...",
				"Carregando vídeo...", false);

		progressDialog.setCancelable(true);
		progressDialog.setOnCancelListener(new OnCancelListener() {

			public void onCancel(DialogInterface dialog) {
				mostrarDialogDeFalha();

			}
		});

		progressDialog.show();

		video.setOnErrorListener(new OnErrorListener() {

			public boolean onError(MediaPlayer mp, int what, int extra) {
				progressDialog.dismiss();
				mostrarDialogDeFalha();
				return true;
			}
		});
		
		video.setOnPreparedListener(new OnPreparedListener() {
			
			public void onPrepared(MediaPlayer arg0) {
				updateScreen();
			}
		});
		
	}

	private synchronized void updateScreen(){
		if(t != null && !t.isAlive()){
			t.stop();
		}
		t = new Thread(new Runnable() {
			
			public void run() {
				video.start();
				progressDialog.dismiss();
				
			}
		});
		t.start();
	}
	@Override
	public void onCreate(Bundle extras) {
		super.onCreate(extras);

		this.mGa = AnalyticsUtils.getInstance(this);

		getWindow().setFormat(PixelFormat.TRANSLUCENT);
		setContentView(R.layout.camera_view);

		RelativeLayout rl = (RelativeLayout) findViewById(R.idCamera.relativeLayout01);

		ControleDeBackground.SetBarraSuperiorBackGround(
				getApplicationContext(), rl);

		ImageView casa = (ImageView) findViewById(R.id.casa);
		casa.setOnClickListener(casaListener);

	}

	private void mostrarDialogDeFalha() {
		Builder a = new AlertDialog.Builder(CameraView.this);

		a.setTitle("Error!")
				.setMessage(
						"Não foi possível carregar o vídeo, verifique sua conexão com a internet!");

		a.setNeutralButton("OK", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {

				//setResult(RESULT_OK);
				//finish();

			}
		});

		a.show();

	}
}
